package io.renren.modules.app.service.impl;

import io.renren.common.utils.R;
import io.renren.modules.app.dao.ImReportDao;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.ImReportEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.ImReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;


@Service("imReportService")
public class ImReportServiceImpl extends ServiceImpl<ImReportDao, ImReportEntity> implements ImReportService {

    @Autowired
    UserDao userDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ImReportEntity> page = this.page(
                new Query<ImReportEntity>().getPage(params),
                new QueryWrapper<ImReportEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R saveOne(UserEntity user, ImReportEntity imReport) {
        if(imReport.getReportedType().equals("user") && imReport.getReportedId()==user.getUserId().intValue()){
            return R.error("不能举报自己");
        }
        if(userDao.selectOne(new QueryWrapper<UserEntity>().eq("user_id",imReport.getReportedId()).eq("status",0))==null){
            return R.error("被举报人不存在");
        }
        if(this.baseMapper.selectOne(new QueryWrapper<ImReportEntity>().eq("reported_id",imReport.getReportedId()).eq("reported_type",imReport.getReportedType()).eq("status","pending").eq("user_id",user.getUserId()))!=null){
            return R.error("请勿反复提交");
        }
        imReport.setUserId(user.getUserId().intValue());
        imReport.setCreatedAt(new Date());
        imReport.setUpdatedAt(new Date());
        this.baseMapper.insert(imReport);
        return R.ok();
    }

}
